package com.lej.core.utils.extention

import android.animation.Animator
import android.animation.ObjectAnimator
import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.appcompat.widget.ContentFrameLayout
import androidx.core.view.children
import androidx.core.view.forEach
import androidx.core.view.forEachIndexed
import androidx.core.view.isVisible
import com.lej.core.R

typealias DealView = (View) -> Unit

//显示loading
fun Activity.showPageStateLoading(block: DealView = {}) {
    val content = window.findViewById<ContentFrameLayout>(android.R.id.content)
    content.showPageState(PageState.Loading(block))
}

//显示loading
fun ViewGroup.showPageStateLoading() {
    this.showPageState(PageState.Loading())
}

sealed class PageState {
    class Empty(
        var str: String,
        val btnText: String = "",
        val btnVisible: Boolean = false,
        val block: DealView = {}
    ) : PageState()

    class Loading(val block: DealView = {}) : PageState()
    class Custom(@LayoutRes val layoutId: Int, val block: DealView = {}) : PageState()
    class Error(var str: String, val block: DealView = {}) : PageState()
}

//显示 状态页面
fun ViewGroup.showPageState(state: PageState) {
    val context = context
    val originChild = children.firstOrNull()
    val stateLayout: Int
    val stateId: Int
    when (state) {
        is PageState.Empty -> {
            stateLayout = R.layout.layout_empty
            stateId = R.id.state_empty
        }
        is PageState.Loading -> {
            stateLayout = R.layout.layout_loading
            stateId = R.id.state_loading
        }
        is PageState.Error -> {
            stateLayout = R.layout.layout_error
            stateId = R.id.state_error
        }
        is PageState.Custom -> {
            stateLayout = state.layoutId
            stateId = state.layoutId
        }
    }
    ///已经展示了该状态  返回
    if (findViewById<View>(stateId)?.isVisible == true) return

    ///实例化加载中布局
    val inflateStateView = { newParentFrame: FrameLayout ->
        fun inflate(block: View.() -> Unit = {}) =
            LayoutInflater.from(context).inflate(stateLayout, newParentFrame, false)
                .apply {
                    id = stateId
                    newParentFrame.addView(this)
                    this.block()
                }
        when (state) {
            is PageState.Empty -> {
                inflate {
                    findViewById<TextView>(R.id.tv_content).text = state.str
                    findViewById<TextView>(R.id.bt_action).apply {
                        text = state.btnText
                        visibility =
                            if (state.btnVisible) View.VISIBLE else View.GONE
                    }
                    state.block(this)
                }
            }
            is PageState.Loading -> {
                inflate {
                    state.block(this)
                }
            }
            is PageState.Error -> {
                inflate {
                    findViewById<TextView>(R.id.tv_content).text = state.str
                    state.block(this)
                }
            }
            is PageState.Custom -> {
                inflate {
                    state.block(this)
                }
            }
        }
    }
    ///新的父容器
    var newStateParentFrame = findViewById<FrameLayout>(R.id.state_content)
    var stateView = findViewById<View>(stateId)
    if (newStateParentFrame != null) {
        if (stateView == null) {
            stateView = inflateStateView(newStateParentFrame)
        }
    } else {
        newStateParentFrame = FrameLayout(context).apply {
            layoutParams = originChild?.layoutParams
            id = R.id.state_content
        }

        removeView(originChild)
        addView(newStateParentFrame)
        ///将原有布局放到第一个
        newStateParentFrame.addView(originChild, 0)
        stateView = inflateStateView(newStateParentFrame)
    }
    /// 展示 stateView  隐藏其他
    newStateParentFrame.forEach { it.isVisible = stateView == it }
}

///需要传入容器父布局,替换掉的只是第一个子节点
fun ViewGroup.restorePageState() {
    this.findViewById<FrameLayout>(R.id.state_content)?.apply { restorePageState() }
}

fun Activity.restorePageState() {
    window.findViewById<FrameLayout>(R.id.state_content)?.apply { restorePageState() }
}

private fun FrameLayout.restorePageState() {
    forEachIndexed { index, view ->
        if (index == 0) {
            ///如果content已经是显示状态,不管返回
            if (view.isVisible) {
                return@forEachIndexed
            }
            view.isVisible = true
            showAnim(view).start()
        } else if (view.isVisible) {
            fadeAnim(view).apply {
                addListener(object : Animator.AnimatorListener {
                    override fun onAnimationRepeat(animation: Animator?) {}
                    override fun onAnimationStart(animation: Animator?) {}
                    override fun onAnimationEnd(animation: Animator?) {
                        view.alpha = 1f
                        view.isVisible = false
                    }

                    override fun onAnimationCancel(animation: Animator?) {
                        view.alpha = 1f
                        view.isVisible = false
                    }
                })
            }.start()
        }
    }
}

private fun fadeAnim(view: View) =
    ObjectAnimator.ofFloat(view, "alpha", 1f, 0f).apply { duration = 250 }

private fun showAnim(view: View) =
    ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).apply { duration = 250 }

